package com.ruoyi.project.system.role.service;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.menu.domain.Menu;
import com.ruoyi.project.system.menu.service.IMenuService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.domain.RoleMenu;
import com.ruoyi.project.system.role.domain.recycle.*;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.domain.UserMenu;
import com.ruoyi.project.system.user.domain.UserRole;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author renzhihao
 * @date 2023/12/08 18:06
 */
@Service
public class RecycleService {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IUserService userService;

    public RecycleResult<IAMRecycleRole> recycleRole(Integer page, Integer pageSize) {
        RecycleResult<IAMRecycleRole> result = new RecycleResult<>();

        Role query = new Role();
        if (page != null && pageSize != null) { // 不分页
            result.setHasNext(0);
            PageUtils.startPage(page, pageSize);
            int total = roleService.selectRoleCount(query);
            if ((page + 1) * pageSize < total) {
                result.setHasNext(1);
            }
        }

        List<Role> roles = roleService.selectRoleList(query);
        List<IAMRecycleRole> iamRecycleRoleList = roles.stream().map(role -> {
            IAMRecycleRole iamRecycleRole = new IAMRecycleRole();
            iamRecycleRole.setName(role.getRoleName());
            iamRecycleRole.setKey(role.getRoleKey());
            iamRecycleRole.setDesc(role.getRemark());
            iamRecycleRole.setSort(Integer.valueOf(role.getRoleSort()));
            iamRecycleRole.setCreateTime(role.getCreateTime());
            iamRecycleRole.setUpdateTime(role.getUpdateTime());
            return iamRecycleRole;
        }).collect(Collectors.toList());

        result.setCode(String.valueOf(AjaxResult.Type.IAM_SUCCESS.value()));
        result.setMsg("回收成功");
        result.setData(iamRecycleRoleList);
        return result;

    }

    public RecycleResult<IAMRecyclePermission> recyclePermission(Integer page, Integer pageSize) {
        RecycleResult<IAMRecyclePermission> result = new RecycleResult<>();

        List<Menu> menus;
        if (page != null && pageSize != null) { // 不分页
            result.setHasNext(0);
            PageUtils.startPage(page, pageSize);
            menus = menuService.selectRecycleMenuList();
            result.setHasNext(new PageInfo<>(menus).isHasNextPage() ? 1 : 0);
        } else {
            menus = menuService.selectRecycleMenuList();
        }

        List<IAMRecyclePermission> iamRecycleRoleList = menus.stream().map(menu -> {
            IAMRecyclePermission iamRecyclePermission = new IAMRecyclePermission();
            iamRecyclePermission.setName(menu.getMenuName());
            iamRecyclePermission.setKey(StringUtils.isNotEmpty(menu.getPerms()) ? menu.getPerms() : String.valueOf(menu.getMenuId()));
            iamRecyclePermission.setParentKey(StringUtils.isNotEmpty(menu.getParentPerms()) ? menu.getParentPerms() :
                    menu.getParentId() == 0 ? null : String.valueOf(menu.getParentId()));
            iamRecyclePermission.setModel(StringUtils.equals("F", menu.getMenuType()) ? "button" : "menu");
            iamRecyclePermission.setDesc(menu.getRemark());
            iamRecyclePermission.setCreateTime(menu.getCreateTime());
            iamRecyclePermission.setUpdateTime(menu.getUpdateTime());
            return iamRecyclePermission;
        }).collect(Collectors.toList());

        result.setCode(String.valueOf(AjaxResult.Type.IAM_SUCCESS.value()));
        result.setMsg("回收成功");
        result.setData(iamRecycleRoleList);
        return result;
    }

    public RecycleResult<IAMRecycleRolePermission> recycleRolePermission(Integer page, Integer pageSize) {
        RecycleResult<IAMRecycleRolePermission> result = new RecycleResult<>();

        if (page != null && pageSize != null) { // 不分页
            result.setHasNext(0);
            PageUtils.startPage(page, pageSize);
            int total = roleService.selectRoleMenuCount();
            if ((page + 1) * pageSize < total) {
                result.setHasNext(1);
            }
        }

        List<RoleMenu> roleMenus = roleService.selectRecycleRoleMenuList();
        List<IAMRecycleRolePermission> iamRecycleRoleList = roleMenus.stream().map(roleMenu -> {
            IAMRecycleRolePermission iamRecycleRolePermission = new IAMRecycleRolePermission();
            iamRecycleRolePermission.setRoleKey(roleMenu.getRoleKey());
            iamRecycleRolePermission.setPermissionKey(StringUtils.isNotEmpty(roleMenu.getPerms()) ?
                    roleMenu.getPerms() : String.valueOf(roleMenu.getMenuId()));
            return iamRecycleRolePermission;
        }).collect(Collectors.toList());

        result.setCode(String.valueOf(AjaxResult.Type.IAM_SUCCESS.value()));
        result.setMsg("回收成功");
        result.setData(iamRecycleRoleList);
        return result;
    }

    public RecycleResult<IAMRecycleRoleAccount> recycleRoleAccount(Integer page, Integer pageSize) {
        RecycleResult<IAMRecycleRoleAccount> result = new RecycleResult<>();

        if (page != null && pageSize != null) { // 不分页
            result.setHasNext(0);
            PageUtils.startPage(page, pageSize);
            int total = roleService.selectRoleAccountCount();
            if ((page + 1) * pageSize < total) {
                result.setHasNext(1);
            }
        }

        List<UserRole> userRoles = roleService.selectRecycleRoleAccountList();
        List<IAMRecycleRoleAccount> iamRecycleRoleAccountList = userRoles.stream().map(userRole -> {
            IAMRecycleRoleAccount iamRecycleRoleAccount = new IAMRecycleRoleAccount();
            iamRecycleRoleAccount.setAccount(userRole.getAccount());
            iamRecycleRoleAccount.setKey(userRole.getRoleKey());
            return iamRecycleRoleAccount;
        }).collect(Collectors.toList());

        result.setCode(String.valueOf(AjaxResult.Type.IAM_SUCCESS.value()));
        result.setMsg("回收成功");
        result.setData(iamRecycleRoleAccountList);
        return result;
    }

    public RecycleResult<IAMRecyclePermissionAccount> recyclePermissionAccount(Integer page, Integer pageSize) {
        RecycleResult<IAMRecyclePermissionAccount> result = new RecycleResult<>();

        if (page != null && pageSize != null) { // 不分页
            result.setHasNext(0);
            PageUtils.startPage(page, pageSize);
            int total = roleService.selectRoleAccountCount();
            if ((page + 1) * pageSize < total) {
                result.setHasNext(1);
            }
        }

        List<UserMenu> userMenus = menuService.selectRecycleMenuAccountList();
        List<IAMRecyclePermissionAccount> iamRecyclePermissionAccountList = userMenus.stream().map(userMenu -> {
            IAMRecyclePermissionAccount iamRecyclePermissionAccount = new IAMRecyclePermissionAccount();
            iamRecyclePermissionAccount.setAccount(userMenu.getAccount());
            iamRecyclePermissionAccount.setKey(userMenu.getPerms());
            return iamRecyclePermissionAccount;
        }).collect(Collectors.toList());

        result.setCode(String.valueOf(AjaxResult.Type.IAM_SUCCESS.value()));
        result.setMsg("回收成功");
        result.setData(iamRecyclePermissionAccountList);
        return result;
    }

    public List<IAMRecycleDept> recycleDept() {
        List<Dept> depts = deptService.selectRecycleDeptList();
        return depts.stream().map(dept -> {
            IAMRecycleDept iamRecycleDept = new IAMRecycleDept();
            iamRecycleDept.setDeptName(dept.getDeptName());
            iamRecycleDept.setDeptNum(dept.getDeptNum());
            iamRecycleDept.setParentNum(dept.getParentNum());
            iamRecycleDept.setLeader(dept.getLeader());
            iamRecycleDept.setPhone(dept.getPhone());
            iamRecycleDept.setEmail(dept.getEmail());
            iamRecycleDept.setOrderNum(dept.getOrderNum());
            iamRecycleDept.setStatus(dept.getStatus());
            return iamRecycleDept;
        }).collect(Collectors.toList());
    }

    public List<IAMRecycleUser> recycleUser() {
        List<User> users = userService.selectRecycleUserList();
        return users.stream().map(user -> {
            IAMRecycleUser iamRecycleUser = new IAMRecycleUser();
            iamRecycleUser.setLoginName(user.getLoginName());
            iamRecycleUser.setUserName(user.getUserName());
            iamRecycleUser.setDeptNums(user.getDept() == null ? null : user.getDept().getDeptNum());
            iamRecycleUser.setEmail(user.getEmail());
            iamRecycleUser.setPhonenumber(user.getPhonenumber());
            iamRecycleUser.setSex(user.getSex());
            return iamRecycleUser;
        }).collect(Collectors.toList());
    }
}
